/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.sensor;

import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import nl.cloudfarming.client.geoviewer.LayerObject;
import nl.cloudfarming.sensor.model.PlotBO;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.openide.util.Exceptions;

/**
 *
 * @author Timon Veenstra
 */
public class PlotDataLayerObject implements LayerObject<MultiPolygon> {

    private final MultiPolygon geometry;
    private final PlotBO plotBO;

    public PlotDataLayerObject(PlotBO plotBO) {
        try {
            GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
            WKTReader reader = new WKTReader(geometryFactory);
            geometry = (MultiPolygon) reader.read(plotBO.getWKT());
            this.plotBO = plotBO;
        } catch (ParseException ex) {
            Exceptions.printStackTrace(ex);
            throw new RuntimeException(ex);
        }
    }

    @Override
    public MultiPolygon getGeometry() {
        return this.geometry;
    }

    @Override
    public String getKey() {
        return Long.toString(this.plotBO.getPlotId());
    }

    @Override
    public String getTooltipText() {
        return Long.toString(this.plotBO.getPlotId()) + " " + this.plotBO.getName();
    }

    @Override
    public Long getKeyAttributeValue() {
        return this.plotBO.getPlotId();
    }

    @Override
    public Long getId() {
        return this.plotBO.getPlotId();
    }
}
